//
//  Course_VC.m
//  Run
//
//  Created by BetaTheta on 16/4/1.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "Course_VC.h"
#import "GeneralModel.h"
#import "GeneralCell.h"
#import "DYAFNetTool.h"
#import "CourseWeb_VC.h"

@interface Course_VC ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UIButton *trainBtn;
@property (nonatomic, strong) UIButton *cultureBtn;

@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) UIScrollView *bgSV;
@property (nonatomic, strong) UITableView *trainTV;/** 动态tableView **/
@property (nonatomic, strong) UITableView *cultureTV;/** 广场tableView **/

@property (nonatomic, strong) NSString *trainString;
@property (nonatomic, strong) NSString *cultureString;
@property (nonatomic, strong) NSString *GeneralString;

@property (nonatomic, strong) NSMutableArray *trainDataArr;
@property (nonatomic, strong) NSMutableArray *cultureDataArr;


@property (nonatomic, assign) NSInteger refreshData;

//@property (nonatomic, assign) NSIn



@end

@implementation Course_VC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"教程";
    self.refreshData = 1;
    [self creatDataOne];
    [self creatDataTwo];
    [self createBgSV];
    [self createTableView];
    [self createTopButton];
    [self createLineView];
    
    self.trainDataArr = [NSMutableArray array];
    self.cultureDataArr = [NSMutableArray array];
    
}
-(void)creatDataOne {
    
    [MBProgressHUD showMessage:@"正在加载"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUD];
        
    });
    self.trainString = [NSString stringWithFormat:@"page=%ld&pagesize=10&subject_id=3", self.refreshData];
    
    
//    提高训练
    [DYAFNetTool POSTNetWithUrl:@"http://media.api.thejoyrun.com/article-list-v1" body:_trainString bodyStyle:DYRequestString headerFile:nil response:DYJSON success:^(id result) {
       
        NSMutableArray *array1 = [GeneralModel modelHanderWithArray:result[@"data"]];
        
//        刷新
        if (self.refreshData == 1) {
            [self.trainDataArr removeAllObjects];
        }
        [self.trainDataArr addObjectsFromArray:array1];
        
        
        
        [_trainTV reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
    }
    
- (void)creatDataTwo {
    self.cultureString = [NSString stringWithFormat:@"page=%ld&pagesize=10&subject_id=5", self.refreshData];

        //   跑步文化
        [DYAFNetTool POSTNetWithUrl:@"http://media.api.thejoyrun.com/article-list-v1" body:_cultureString bodyStyle:DYRequestString headerFile:nil response:DYJSON success:^(id result) {
            
            NSMutableArray *array2 = [GeneralModel modelHanderWithArray:result[@"data"]];
            
            
            if (self.refreshData == 1) {
                [self.cultureDataArr removeAllObjects];
            }
            [self.cultureDataArr addObjectsFromArray:array2];
            
            [_cultureTV reloadData];
        } failure:^(NSError *error) {
            
        }];
    
        
    }
    
- (void)createTopButton{
    // 动态btn
    self.trainBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _trainBtn.frame = CGRectMake(0, 0, WIDTH / 2, 30);
    [_trainBtn setTitle:@"提高训练" forState:(UIControlStateNormal)];
    [_trainBtn setTitleColor:[UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000] forState:(UIControlStateNormal)];
    [_trainBtn addTarget:self action:@selector(goToDynamicTV) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:_trainBtn];
    // 广场btn
    self.cultureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _cultureBtn.frame = CGRectMake(WIDTH / 2, 0, WIDTH / 2, 30);
    [_cultureBtn setTitle:@"跑步文化" forState:(UIControlStateNormal)];
    [_cultureBtn addTarget:self action:@selector(goToSquareTV) forControlEvents:UIControlEventTouchUpInside];
    [_cultureBtn setTitleColor:[UIColor colorWithWhite:0.603 alpha:1.000] forState:(UIControlStateNormal)];
    
    [self.view addSubview:_cultureBtn];
    
}
#pragma TOP BUTTON 点击方法
- (void)goToDynamicTV {
    
    [_trainBtn setTitleColor:[UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000] forState:(UIControlStateNormal)];
    [_cultureBtn setTitleColor:[UIColor colorWithWhite:0.603 alpha:1.000] forState:(UIControlStateNormal)];
    _lineView.center = CGPointMake( WIDTH / 4, _lineView.center.y);
    //设置scrollView的偏移量
    [self.bgSV setContentOffset:CGPointMake(0, 0) animated:YES];
    
    NSLog(@"提高");
}
- (void)goToSquareTV {
    
    
    [_cultureBtn setTitleColor:[UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000] forState:(UIControlStateNormal)];
    [_trainBtn setTitleColor:[UIColor colorWithWhite:0.603 alpha:1.000] forState:(UIControlStateNormal)];
    _lineView.center = CGPointMake( WIDTH * 3 / 4, _lineView.center.y);
    //设置scrollView的偏移量
    [self.bgSV setContentOffset:CGPointMake(WIDTH, 0) animated:YES];
    NSLog(@"文化");
    
}
#pragma mark - 创建滚动指示条
- (void)createLineView {
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(50, 33, WIDTH / 4, 3)];
    _lineView.backgroundColor = [UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000];
    [self.view addSubview:_lineView];
    
}
#pragma mark - 创建底层滚动视图
// backgroundScollView
- (void)createBgSV {
    
    self.bgSV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _bgSV.contentSize = CGSizeMake(WIDTH * 2, 0);
    /**< pagingEnable整页翻动 */
    _bgSV.pagingEnabled = YES;
    _bgSV.showsHorizontalScrollIndicator = YES;
    _bgSV.showsVerticalScrollIndicator = NO;
    _bgSV.bounces = NO;
    
    // 设置代理
    _bgSV.delegate = self;
    
    _bgSV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_bgSV];
    
    // 创建scrollView上的tableView
    //    [self createTableView];
    
}
//按钮颜色 偏移量联动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (_bgSV.contentOffset.x == 0){
        [self goToDynamicTV];
        
        
    }
    if (_bgSV.contentOffset.x == WIDTH ) {
        [self goToSquareTV];
        
    }
}

#pragma mark - 创建scrollView上的tableView
- (void)createTableView{
    
    /**< 动态 */
    self.trainTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 35, WIDTH, HEIGHT - 60) style:UITableViewStylePlain];
    _trainTV.backgroundColor = [UIColor colorWithRed:0.933 green:0.898 blue:0.820 alpha:0.500];
    _trainTV.pagingEnabled = NO;
    _trainTV.showsHorizontalScrollIndicator = NO;
    _trainTV.showsVerticalScrollIndicator = YES;
    
    _trainTV.delegate = self;
    _trainTV.dataSource = self;
    [_trainTV registerClass:[GeneralCell class] forCellReuseIdentifier:@"GeneralCellIdentifier"];
//    刷新
    self.trainTV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _refreshData = 1;
        [self creatDataOne];
        [_trainTV reloadData];
        [_trainTV.mj_header endRefreshing];
    }];
    
    
//    加载
    self.trainTV.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _refreshData += 1;
        [self creatDataOne];
        [_trainTV reloadData];
        [_trainTV.mj_footer endRefreshing];
    }];
    
    
    
    [_bgSV addSubview:_trainTV];
    
    
    /**< 广场 */
    self.cultureTV = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH, 35, WIDTH, HEIGHT - 60) style:UITableViewStylePlain];
    _cultureTV.backgroundColor = [UIColor colorWithRed:0.933 green:0.898 blue:0.820 alpha:0.500];
    _cultureTV.pagingEnabled = NO;
    _cultureTV.showsHorizontalScrollIndicator = NO;
    _cultureTV.showsVerticalScrollIndicator = YES;
    _cultureTV.delegate = self;
    _cultureTV.dataSource = self;
    [_cultureTV registerClass:[GeneralCell class] forCellReuseIdentifier:@"GeneralCellIdentifier"];
    
    //    刷新
    self.cultureTV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _refreshData = 1;
        [self creatDataTwo];
        [_cultureTV reloadData];
        [_cultureTV.mj_header endRefreshing];
    }];
    
    
    //    加载
    self.cultureTV.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _refreshData += 1;
        [self creatDataTwo];
        [_cultureTV reloadData];
        [_cultureTV.mj_footer endRefreshing];
    }];

    
    
      [_bgSV addSubview:_cultureTV];
    
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _trainTV) {
        return _trainDataArr.count;
    } else {
        return _cultureDataArr.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _trainTV) {
        return 100;
    }else {
        return 100;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _trainTV) {
       GeneralCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GeneralCellIdentifier"];
        
        cell.generalModel = _trainDataArr[indexPath.row];
        
        return cell; 
    }else {
        GeneralCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GeneralCellIdentifier"];
        cell.generalModel = _cultureDataArr[indexPath.row];
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _trainTV) {
        GeneralModel *generealModel = _trainDataArr[indexPath.row];
        
        
        CourseWeb_VC *CVC = [[CourseWeb_VC alloc] init];
        CVC.keyStr = generealModel.article_url;
        
        [self.navigationController pushViewController:CVC animated:YES];
    }else {
        GeneralModel *generealModel = _cultureDataArr[indexPath.row];
        CourseWeb_VC *CVC = [[CourseWeb_VC alloc] init];
        CVC.keyStr = generealModel.article_url;
        
        [self.navigationController pushViewController:CVC animated:YES];
        
    }
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
